using System;
using System.Collections.Generic;
using System.ComponentModel;
using Cmc.Modbus;
using Cmc.Modbus.Base;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using NewLife;
using NewLife.Log;
using NewLife.Net;
using NewLife.Serialization;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            XTrace.UseConsole();

            NetServer ns = new NetServer(IPAddress.Parse("192.168.3.88"), 8282);
            ns.Log = XTrace.Log;
            ns.NewSession += Ns_NewSession;
            ns.Received += Ns_Received;
            ns.Start();

            IClientFactory defaultClientFactory = new DefaultClientFactory();
            IBytesCmd bytesCmd = new ModbusRtuCmd(); 

            DefaultReadWrite rw = new DefaultReadWrite(bytesCmd, defaultClientFactory);
 
            Console.Read();
              var read = new ReadData()
            {
                FunctionType = FunctionType.HoldingRegister,   //读取寄存器类型
                Ip = "192.168.3.87",                                            //IP地址
                Port = 1468,                                                            //网络端口
                TerminalFlag = 1,                                                   //分站端口/分站号
                CommunicationType = CommunicationType.UdpServer,  //UDPServer传输
                Server = ns    //使用udp server 必须要赋值
            };

            //添加需要读取的地址
            read.ReadAddresses = new List<AddressRead>();
            read.ReadAddresses.Add(new AddressRead(0));
            read.ReadAddresses.Add(new AddressRead(1));
            //read.ReadAddresses.Add(new AddressRead(5));
            //read.ReadAddresses.Add(new AddressRead(8));
            //read.ReadAddresses.Add(new AddressRead(15, 3));

            //read.ReadAddresses.AddReadAddressesFrom(16, 8);  //读取地址添加起始16，长度8的地址

            var readbacks = rw.ReadData(read);
            List<ReadDataBack> readbacks2 = rw.ReadData(read);

            foreach (var readDataBack in readbacks2)
            {
                foreach (var dic in readDataBack.Addresses)
                {
                    Console.WriteLine(dic.Key + "              " + dic.Value.ValueInt);
                }
            }

            string va = readbacks2.ToJson();


            Console.Read();

            var write = new WriteData()
            {
                FunctionType = FunctionType.HoldingRegister,
                Ip = "192.168.0.111",
                Port = 502
            };
            write.WriteDatas = new List<AddressWriteValue>();
            write.WriteDatas.Add(new AddressWriteValue(1, 0, 0));
            //write.WriteDatas.Add(new AddressWriteValue(1, 1, 0));
            //write.WriteDatas.Add(new AddressWriteValue(1, 1, 15));
            write.WriteDatas.Add(new AddressWriteValue(2, 99));

           // rw.WriteData(write);


            Console.WriteLine(readbacks2.ToJson());
            Console.ReadLine();
        }

        private static void Ns_NewSession(object sender, NetSessionEventArgs e)
        {
           Console.WriteLine(e.Session .Remote.Port);
        }

        private static void Ns_Received(object sender, ReceivedEventArgs e)
        {
            //Console.WriteLine(e.Packet.ReadBytes(0, e.Packet.Count).ToHex(" "));
        }
    }
}
